<?php

/**
 * IndexController - The default controller class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class CategoryController extends App_Zend_Controller_Action_Shop
{
	
	public function init()
	{
		$this->view->styleName = 'product-list';	
	}
	
    public function detailAction()
    {
        $id = $this->_request->getParam('cid');   
        $idArray[0] = intval($id);
		$idArray[1] = 'ok';
        if($idArray[1] == '') {
            $this->_forward('index','error');
        } else {
        	        	
            $page=$this->_request->getParam('page',1);
            $order=$this->_request->getParam('order',0);
            $cid = $idArray[0];
            $this->view->assign('cssid',$cid);
			
			
            $productFl = new CFenlei('product');
            $flArray = $this->getHelper('Fenlei')->getFenleiChild($productFl->getFenleiTree(),$cid);           
			$currentFenlei = $productFl->getoneFenlei($cid);
            if($flArray) {
                $where=R_PRE."items.flid in (".join(',',$flArray).') and is_showed=1 and zcshop_products.onsales=0';
            } else {
                $where=R_PRE."items.flid in (".$idArray[0].') and is_showed=1 and zcshop_products.onsales=0';
            }
            
            $f = $this->getRequest()->getParam('f', 0);
            $attr = new CAttr();
            if ($f) {
            	$f = base64_decode($f);     
            		
            	$filterStr = explode(',', $f);
            	$filter = array();
            	foreach ($filterStr as $v) {
            		$row = '';
            		$row = $attr->find($v)->current();
            		if ($row->type == '1') {
            			$filter['category'] = $v;
            		} elseif ($row->type == '2') {
            			$filter['brand'] = $v;
            		} elseif ($row->type == '3') {
            			$filter['player'] = $v;
            		} else {
            			exit();
            		}
            	}
            } else {
            	$filter = array();
            }
            $filterIds = array_values($filter);
			sort($filterIds);
			$subfix = '';
			if ($filterIds) {
				$subfix = base64_encode(implode(',', $filterIds));
			}
            $itemShow=new CItemShow();
            $itemShow->setType('product');
            $itemShow->setWhere($where);
            $itemCounts=$itemShow->getAllItemCounts($filter);
            Zend_Loader::loadClass("CFilter");
            $pageArray = CFilter::filterPage ( $page, $itemCounts, 36 );
            $itemShow->setFromTo(array('counts'=>36,'from'=>$pageArray['from']));
            $myorder='';
            if($order == '0') {
                $myorder = array(R_PRE."items.px ASC",R_PRE."items.view_num DESC");
            } elseif ($order == '1') {
                $myorder = R_PRE."products.min_price DESC";
            } elseif($order == '2') {
                $myorder = R_PRE."products.min_price ASC";
            }
            $itemShow->setOrder($myorder);
            $itemArray=$itemShow->getAllItems($filter);
            
            $cflObj = $productFl->getFenleiById($id);
       		if ($cflObj[0]['pid'] == 93) {
            	$this->view->isNational = 1;
            }
            $this->getHelper('Fenlei')->getCategories();
            Zend_Loader::loadClass("CPage");
            $this->view->assign("pageArray", $pageArray );
            $this->view->assign('itemArray',$itemArray);
            $this->view->assign('itemCounts',$itemCounts);
            $this->view->assign('order',$order);
            $this->view->assign('page',$page);
            $parentArray=$this->getHelper('Fenlei')->getAllParents($this->view,$cid);
           
            $this->view->currentFenlei = $currentFenlei;            
            $navFlString=join(' &gt;&gt; ',$parentArray);
			$pageString = CPage::categoryDivPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/type-$id-$order", $this->view->encodeName(trim($this->view->toText($parentArray[count($parentArray)-1]))), $subfix);
			$this->view->assign ( "pageString", $pageString );
            $this->view->assign ( "parentArray", $parentArray );
            $this->view->assign ( "navFlString", $this->view->toText($parentArray[count($parentArray)-1]));

            //$this->view->webSiteDescription = $this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]);
			
			if($page == 1 && $f == ''){
				$product = new CProduct();
				$topProductArray = $product->getTopProduct($id, 0, 4);
				$this->view->topProductArray = $topProductArray;
			}
			
			//只筛选出这个分类下所有产品有的属性，没有的属性不选出来
			//$productAttributeMap = CCache::getProductAttributeMap();
			//$this->view->productAttributeMap = $productAttributeMap;
		
			//用户选择过的选项
			//$this->view->selectedProductAttributes = array(5=>'Alexis Sanchez',6=>'Gerard Pique',8=>'Adidas',10=>'Jersey Shirt');
			$productAttributeMap = '';			
			
			$this->view->filter = $filter;			
			$this->view->filterIds = $filterIds;
			
			$filterType = array_keys($filter);
			if (!in_array('category', $filterType)) {	
				$categoryAttr = $attr->getByType(1);		
				foreach ($categoryAttr as $v) {						
					$tmp = $filter;
					$tmp['category'] = $v->attr_id;				
					if ($attr->checkProduct($where, $tmp)) {
						$productAttributeMap['Category'][] = $v;
					}
				}
			}
			if (!in_array('brand', $filterType)) {
	        	$brandAttr = $attr->getByType(2);
				foreach ($brandAttr as $v) {
					$tmp = $filter;
					$tmp['brand'] = $v->attr_id;				
					if ($attr->checkProduct($where, $tmp)) {
						$productAttributeMap['Brand'][] = $v;
					}
				}
			}
			if (!in_array('player', $filterType)) {
	        	$playerAttr = $attr->getByType(3);
				foreach ($playerAttr as $v) {
				$tmp = $filter;
					$tmp['player'] = $v->attr_id;				
					if ($attr->checkProduct($where, $tmp)) {
						$productAttributeMap['Player'][] = $v;
					}
				}
			}
						
			$this->view->productAttributeMap = $productAttributeMap;
			foreach ($filter as $v) {
				$selectedProductAttributes[] = $attr->find($v)->current();
			}
            $this->view->selectedProductAttributes = $selectedProductAttributes;
            
            $attrSeo = '';
            if(is_array($selectedProductAttributes)){
            	foreach($selectedProductAttributes as $v){
            		$attrSeo = $v->attr_name.', '.$attrSeo;
            	}
            }
            
            $fenlei = new CFenlei('product');
            $current = $fenlei->find($id)->current();
            if (!empty($current->title)) {
            	if($page == 1){
            		if($attrSeo!=''){
            			$this->view->webSiteDescription = $attrSeo.$current->title;
            		}else{
            			$this->view->webSiteDescription = $current->fenlei_description;
            		}
            	}else{
            		$this->view->webSiteDescription = $attrSeo.'Page '.$page.', Soccer Jersey Wholesale at fancycheapjersey';
            	}
	            $this->view->webSiteTitle =  $attrSeo.$current->title . ',cheap soccer jersey Wholesale, Page '.$page;
	            $this->view->webSiteKeywords = $attrSeo.$current->fenlei_keywords;
            } else {
            	$this->view->webSiteTitle = $attrSeo.$this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2])  . ',cheap soccer jersey Wholesale, Page '.$page;
            	$this->view->webSiteKeywords = $attrSeo.$this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]) . ',cheap Jersey Shirt Free Shipping';
            	$this->view->webSiteDescription = $attrSeo.$this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]) .',Soccer Jersey Wholesale cheap price in fancycheapjersey.com';
            }
			if(empty($this->view->webSiteDescription)){
				$this->view->webSiteDescription=strip_tags($parentArray[count($parentArray)-1]).",".strip_tags($parentArray[count($parentArray)-2]).",".strip_tags($parentArray[count($parentArray)-1])." cheap soccer jersey Wholesale, cheap soccer jersey Wholesale ";
			}
            
            //SET META FOR FACEBOOK
	        $this->getHelper ('layout')->getView()->headMeta()->setName('og:title', $this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]) . ',Free Shipping');
			$this->getHelper ('layout')->getView()->headMeta()->setName('og:type', 'jersery');
			$this->getHelper ('layout')->getView()->headMeta()->setName('og:url', MY_URL);
			$this->getHelper ('layout')->getView()->headMeta()->setName('og:image', MY_URL.'/images/logo.jpg');
			$this->getHelper ('layout')->getView()->headMeta()->setName('og:site_name', 'fancycheapjersey.com');
			$this->getHelper ('layout')->getView()->headMeta()->setName('fb:app_id', FACEBOOK_CONSUMER_ID);
			$this->getHelper ('layout')->getView()->headMeta()->setName('og:description', $this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]) . ',Free Shipping');
        }
        
        $keyModel = new CKeywords();
        $this->view->keyModel = $keyModel;
        $this->view->cid = $cid;
    }
    
    public function showattrbyfidAction(){
    	
    	$this->getHelper('layout')->disableLayout();
    	
    	$id = $this->_request->getParam('cid');
    	$idArray[0] = intval($id);
    	$cid = $idArray[0];
    	$productFl = new CFenlei('product');
        $flArray = $this->getHelper('Fenlei')->getFenleiChild($productFl->getFenleiTree(),$cid);           
		$currentFenlei = $productFl->getoneFenlei($cid);
		$this->view->currentFenlei = $currentFenlei;
        if($flArray) {
            $where=R_PRE."items.flid in (".join(',',$flArray).') and is_showed=1 and zcshop_products.onsales=0';
        } else {
            $where=R_PRE."items.flid in (".$idArray[0].') and is_showed=1 and zcshop_products.onsales=0';
        }
        
        $filter = array();

        $attr = new CAttr();
        $filterType = array_keys($filter);
		if (!in_array('category', $filterType)) {	
			$categoryAttr = $attr->getByType(1);		
			foreach ($categoryAttr as $v) {						
				$tmp = $filter;
				$tmp['category'] = $v->attr_id;				
				if ($attr->checkProduct($where, $tmp)) {
					$productAttributeMap['Category'][] = $v;
				}
			}
		}
		if (!in_array('brand', $filterType)) {
        	$brandAttr = $attr->getByType(2);
			foreach ($brandAttr as $v) {
				$tmp = $filter;
				$tmp['brand'] = $v->attr_id;				
				if ($attr->checkProduct($where, $tmp)) {
					$productAttributeMap['Brand'][] = $v;
				}
			}
		}
		if (!in_array('player', $filterType)) {
        	$playerAttr = $attr->getByType(3);
			foreach ($playerAttr as $v) {
			$tmp = $filter;
				$tmp['player'] = $v->attr_id;				
				if ($attr->checkProduct($where, $tmp)) {
					$productAttributeMap['Player'][] = $v;
				}
			}
		}
					
		$this->view->productAttributeMap = $productAttributeMap;
		$this->view->cid = $cid;
    	
    }
    
    public function neworderAction()
    {
    	$this->getHelper('Fenlei')->getCategories();
    	$page=$this->_request->getParam('page',1);
    	$productFl = new CFenlei('product');
        $where="is_showed=1 and zcshop_products.onsales=0";
        $itemShow=new CItemShow();
        $itemShow->setType('product');
        $itemShow->setWhere($where);
        $itemCounts=$itemShow->getAllItemCounts();
        Zend_Loader::loadClass("CFilter");
        $pageArray = CFilter::filterPage ( $page, $itemCounts, 36 );
        $itemShow->setFromTo(array('counts'=>36,'from'=>$pageArray['from']));
        $myorder='';
        $myorder = R_PRE."items.add_time DESC";
        $itemShow->setOrder($myorder);
        $itemArray=$itemShow->getAllItems();
        $this->view->assign('itemArray',$itemArray);
        $this->view->assign('itemCounts',$itemCounts);  
        $pageString = CPage::categoryDivPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/cno",'new_jerseys');          
		$this->view->pageString = $pageString;
        //$this->view->webSiteDescription = $this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]);
        $this->view->webSiteTitle = 'New jerseys,Free Shipping';
        $this->view->webSiteKeywords = 'New soccer jersey,jersey shirts,Free Shipping' ;
		$this->view->webSiteDescription = 'Shop for new soccer jersey';
    }
    
	public function vieworderAction()
    {
    	$this->getHelper('Fenlei')->getCategories();
    	$productFl = new CFenlei('product');
        $where="is_showed=1 and zcshop_products.onsales=0";
        $itemShow=new CItemShow();
        $itemShow->setType('product');
        $itemShow->setWhere($where);
        $itemCounts=$itemShow->getAllItemCounts();
        Zend_Loader::loadClass("CFilter");
        $pageArray = CFilter::filterPage ( $page, $itemCounts, 36 );
        $itemShow->setFromTo(array('counts'=>36,'from'=>$pageArray['from']));
        $myorder='';
        $myorder = R_PRE."items.view_num DESC";
        $itemShow->setOrder($myorder);
        $itemArray=$itemShow->getAllItems();
        $this->view->assign('itemArray',$itemArray);
        $this->view->assign('itemCounts',$itemCounts);            

        //$this->view->webSiteDescription = $this->view->toText($parentArray[count($parentArray)-1]) . ',' . $this->view->toText($parentArray[count($parentArray)-2]);
        $pageString = CPage::categoryDivPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/cno",'hot_jerseys');          
		$this->view->pageString = $pageString;
        $this->view->webSiteTitle = 'Hot jerseys,Free Shipping';
        $this->view->webSiteKeywords = 'Hot soccer jersey,jersey shirts,Free Shipping' ;
		$this->view->webSiteDescription = 'Shop for hot soccer jersey';
    }


    public function newproductAction()
    {

        $page=$this->_request->getParam('page',1);

        $order=$this->_request->getParam('order',0);


        $item=new CItem();

        $isNewItemArray=$item->getAllItems(0,8,'product',0,0,"has_banned","type='2'",'view_num DESC');

        $this->view->assign('isNewItemArray',$isNewItemArray);

        $this->getHelper('Fenlei')->getCategories();


        $itemShow=new CItemShow();

        $itemShow->setType('product');

        //$itemShow->setWhere($where);

        $itemCounts=$itemShow->getAllItemCounts();

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $itemCounts, 36 );

        $itemShow->setFromTo(array('counts'=>36,'from'=>$pageArray['from']));

        $myorder='';

        if($order=='0')
        {

            $myorder=R_PRE."items.add_time DESC";

        }elseif($order=='1')
        {

            $myorder=R_PRE."products.min_price DESC";

        }elseif($order=='2')
        {

            $myorder=R_PRE."products.min_price ASC";

        }

        $itemShow->setOrder($myorder);

        $itemArray=$itemShow->getAllItems();

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/category-newproduct/?order=$order");

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign('itemArray',$itemArray);

        $this->view->assign('itemCounts',$itemCounts);

        $this->view->assign('order',$order);

        $this->view->assign('page',$page);

        $bulletinArray=$item->getAllItems(0,36,'bulletin');

        $this->view->assign('bulletinArray',$bulletinArray);

        $this->view->assign ( "website_description", 'New jerseys' );

        $this->view->assign ( "website_title", 'New jerseys');

        $this->render('items');

    }


    public function hotproductAction()
    {

        $page=$this->_request->getParam('page',1);

        $order=$this->_request->getParam('order',0);


        $item=new CItem();

        $isNewItemArray=$item->getAllItems(0,8,'product',0,0,"has_banned","type='2'",'view_num DESC');

        $this->view->assign('isNewItemArray',$isNewItemArray);

        $this->getHelper('Fenlei')->getCategories();


        $itemShow=new CItemShow();

        $itemShow->setType('product');

        $where="type='1'";

        $itemShow->setWhere($where);

        $itemCounts=$itemShow->getAllItemCounts();

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $itemCounts, 36 );

        $itemShow->setFromTo(array('counts'=>36,'from'=>$pageArray['from']));

        $myorder='';

        if($order=='0')
        {

            $myorder=R_PRE."items.add_time DESC";

        }elseif($order=='1')
        {

            $myorder=R_PRE."products.min_price DESC";

        }elseif($order=='2')
        {

            $myorder=R_PRE."products.min_price ASC";

        }

        $itemShow->setOrder($myorder);

        $itemArray=$itemShow->getAllItems();

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/category-hotproduct/?order=$order");

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign('itemArray',$itemArray);

        $this->view->assign('itemCounts',$itemCounts);

        $this->view->assign('order',$order);

        $this->view->assign('page',$page);

        $bulletinArray=$item->getAllItems(0,36,'bulletin');

        $this->view->assign('bulletinArray',$bulletinArray);

        $this->view->assign ( "website_description", 'Hot jerseys' );

        $this->view->assign ( "website_title", 'Hot jerseys');

        $this->render('items');

    }
	
	
	public function topjerseysAction()
    {
    	$this->getHelper('Fenlei')->getCategories();
    	$page=$this->_request->getParam('page',1);
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();

		CMd5cmn::$showNumber = 30;
		$itemArray = CMd5cmn::combinActionMap($itemMap,date('Y-m-d'));
		$this->view->assign('itemArray',$itemArray);

        $this->view->webSiteTitle = 'Top jerseys,Free Shipping';
        $this->view->webSiteKeywords = 'Top soccer jersey,jersey shirts,Free Shipping' ;
		$this->view->webSiteDescription = 'Shop for top soccer jersey';
    }

	public function combinlistAction(){
		$page=$this->_request->getParam('page',1);
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();
		$this->getHelper('Fenlei')->getCategories();

		CMd5cmn::$showNumber = 40;
		$items = CMd5cmn::combinActionMap($itemMap,$page."-".date('Y-m-d'));
		$resultItemArray = array();
		for($i=0;$i<40;$i++){
			if(!is_array($resultItemArray[$i/4])){
				$resultItemArray[$i/4] = array();
			}
			array_push($resultItemArray[$i/4],$items[$i]);
		}

		$price=new CProductPrice();
		$resultPriceArray = array();
		$resultImgArray = array();
		
		foreach ($resultItemArray as $k=>$v){
			$tmpArr = array();
			$tmpImgArr = array();
			foreach ($v as $_v) {
				//if (!$priceArray = $myCache->load('price_' . $_v['itemid'])) {
					$priceArray = $price->getAllPrice($_v['itemid']);
				//}
				//$myCache->save($priceArray, 'price_' . $_v['itemid']);
				$tmpImgArr[] = $_v['cut_url'];
				foreach ($priceArray as $__v) {
					//if ($__v['max_counts'] < 4) {
						if (isset($tmpArr['price_org'])) {
							$tmpArr['price_org'] += $__v['net_price'];
						} else {
							$tmpArr['price_org'] = $__v['net_price'];							
						}
						break;
					//}
				}
				foreach ($priceArray as $__v) {
					if ($__v['min_counts'] <= 4 && $__v['max_counts'] >= 4) {
						if (isset($tmpArr['price_pack'])) {
							$tmpArr['price_pack'] += $__v['net_price'];
						} else {
							$tmpArr['price_pack'] = $__v['net_price'];							
						}
						break;
					}
				}				
			}
			$resultPriceArray[$k] = $tmpArr;
			$resultImgArray[$k] = implode('|', $tmpImgArr);			
		}
		//计算天数
		$date_1="2012-10-23";   
		$date_2="2012-09-28"; 
		$d1=strtotime($date_1);   
		$d2=strtotime($date_2);
		$days=round(($d1-$d2)/3600/24); 

		$num = (int)(ord(md5($date_1))/10);

		$pageArray = CFilter::filterPage ( $page, 200+$days*200+$num*10, 10 );
		$pageString = CPage::categoryDivPages( $pageArray ['pageNumber'], $pageArray ['page'], MY_URL."/cmn",'package_jerseys');
		$this->view->pageString = $pageString;
		$this->view->resultItemArray = $resultItemArray;
		$this->view->resultPriceArray = $resultPriceArray;
		$this->view->resultImgArray = $resultImgArray;
		
		$this->view->webSiteDescription = 'The 4 in 1 package promotion is according to the integration of every clients\' hobby survey results and buying habit.The bundles you get will be at least 10% cheaper than separate ordered soccer jerseys.';
		$this->view->webSiteTitle =  'Soccer jersey package deals, 4 in 1 package for soccer jerseys on sale.';
		$this->view->webSiteKeywords = 'Package,Soccer Jerseys,Bargains';

	}
	
	function microtime_float()
	{
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}
	
	public function euroAction(){
		$this->getHelper('Fenlei')->getCategories();
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();
		$czhuanti = new CZhuanti();
		$id = 2;
		$zhuanti = $czhuanti->getById($id);
		$itemids = $zhuanti->itemids;
		if($itemids){
			$itemidArray = explode(',', $itemids);
			$itemArray = array();
			foreach($itemidArray as $key => $v){
				$itemArray[] = $itemMap[$v];
			}
		}
		$this->view->itemArray = $itemArray;
		
		if($zhuanti){
			$this->view->webSiteTitle = $zhuanti->seo_title;
			$this->view->webSiteKeywords = $zhuanti->seo_keywords;
			$this->view->webSiteDescription = $zhuanti->seo_description;
		}
	}

    public function postAction()
    {

    //$this->_helper->viewRenderer->setNoRender();

    //$this->getHelper ('layout')->disableLayout ();

    }



}
